package com.nasarius.bank.scorecard.web.controller.surveyor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.nasarius.bank.scorecard.dao.SurveyAnswersDAO;
import com.nasarius.bank.scorecard.dao.SurveyDetailsDAO;
import com.nasarius.bank.scorecard.dao.TopicsDAO;
import com.nasarius.bank.scorecard.domain.SurveyAnswers;
import com.nasarius.bank.scorecard.domain.SurveyDetails;
import com.nasarius.bank.scorecard.domain.Topics;
import com.nasarius.bank.vo.SurveyAnswersVO;
@Controller
public class SurveyorController {
	protected final Log LOG = LogFactory.getLog(getClass());
	@Autowired
	private SurveyDetailsDAO surveyDetailsDAO;
	@Autowired
	private SurveyAnswersDAO surveyAnswersDAO;	
	@Autowired
	private TopicsDAO topicsDAO;	
	@InitBinder
	protected void initBinder(ServletRequestDataBinder binder) {
			binder.registerCustomEditor(List.class,new CustomCollectionEditor(List.class));
	}
    @RequestMapping(value="/surveyordetails/view", method=RequestMethod.GET)
	public void newRequest(Model model) {
		List<SurveyDetails> allSurveyDetails =null;		
		allSurveyDetails = surveyDetailsDAO.selectAllCompletedSurveyDetails(SecurityContextHolder.getContext().getAuthentication().getName());
		model.addAttribute("allSurveyDetails", allSurveyDetails);
	}
    @RequestMapping(value="/surveyordetails/edit", method=RequestMethod.GET)
    public SurveyAnswersVO modifyAnswers(@RequestParam(required = false) Integer surveyId, HttpServletRequest r){
		SurveyAnswersVO surveyAnswersVO = (SurveyAnswersVO)r.getSession().getAttribute("surveyAnswersVO");
    	List<Topics> topicList = topicsDAO.selectAllTopics();
    	if(topicList != null){
    		if(surveyAnswersVO == null){
    			surveyAnswersVO = setFirstTopic(surveyId, topicList);	
			}
    		else if(StringUtils.equals("Y",surveyAnswersVO.getPreviousButtonClicked())){
    			setPreviousTopic(surveyId, surveyAnswersVO, topicList);
    		}else{
    			setNextTopic(surveyId, surveyAnswersVO, topicList);
    		}
    	}
    	r.getSession().removeAttribute("surveyAnswersVO");
    	surveyAnswersVO.setSurveyID(surveyId.toString());
		return surveyAnswersVO;
    }
	private void setPreviousTopic(Integer surveyId,
			SurveyAnswersVO surveyAnswersVO, List<Topics> topicList) {
		Integer previousTopicID = 0;
		surveyAnswersVO.setNextTopicID(surveyAnswersVO.getCurrentTopicID());
		surveyAnswersVO.setCurrentTopicID(surveyAnswersVO.getPreviousTopicID());
		surveyAnswersVO.setSurveyAnswers(surveyAnswersDAO.selectSurveyAnswersBySurveyID(SecurityContextHolder.getContext().getAuthentication().getName(),surveyId, surveyAnswersVO.getCurrentTopicID()));
		for(int i=0; i < topicList.size(); i++){
			Topics topics = topicList.get(i);
			Integer topicID = topics.getTopicid();		
			if(!surveyAnswersVO.getCurrentTopicID().equals(topicID)){
				List<SurveyAnswers> answersList = surveyAnswersDAO.selectSurveyAnswersBySurveyID(SecurityContextHolder.getContext().getAuthentication().getName(),surveyId, topicID);
				if(answersList != null && answersList.size() > 0){
					previousTopicID = topicID;
				}
			}else{
				surveyAnswersVO.setTopicName(topics.getTopicname());
				surveyAnswersVO.setPreviousTopicID(previousTopicID);
				break;
			}
		}
	}
	private void setNextTopic(Integer surveyId,
			SurveyAnswersVO surveyAnswersVO, List<Topics> topicList) {
		surveyAnswersVO.setPreviousTopicID(surveyAnswersVO.getCurrentTopicID());
		surveyAnswersVO.setCurrentTopicID(surveyAnswersVO.getNextTopicID());
		surveyAnswersVO.setNextTopicID(0);
		surveyAnswersVO.setSurveyAnswers(surveyAnswersDAO.selectSurveyAnswersBySurveyID(SecurityContextHolder.getContext().getAuthentication().getName(),surveyId, surveyAnswersVO.getCurrentTopicID()));
		for(int i=0; i < topicList.size(); i++){
			Topics topics = topicList.get(i);
			Integer topicID = topics.getTopicid();		
			if(surveyAnswersVO.getCurrentTopicID().equals(topicID)){
				surveyAnswersVO.setTopicName(topics.getTopicname());
				for(int j=i+1; j < topicList.size(); j++){
					Topics nextTopics = topicList.get(j);
					Integer nextTopicID = nextTopics.getTopicid();
					List<SurveyAnswers> answersList = surveyAnswersDAO.selectSurveyAnswersBySurveyID(SecurityContextHolder.getContext().getAuthentication().getName(),surveyId, nextTopicID);
					if(answersList != null && answersList.size() > 0){
						surveyAnswersVO.setNextTopicID(nextTopicID);
						break;
					}
				}
				break;
			}
		}
	}
	private SurveyAnswersVO setFirstTopic(Integer surveyId,
			List<Topics> topicList) {
		SurveyAnswersVO surveyAnswersVO = new SurveyAnswersVO();
		for(int i=0; i < topicList.size(); i++){
			Topics topics = topicList.get(i);
			Integer topicID = topics.getTopicid();
			List<SurveyAnswers> answersList = surveyAnswersDAO.selectSurveyAnswersBySurveyID(SecurityContextHolder.getContext().getAuthentication().getName(),surveyId, topicID);
			if(answersList != null && answersList.size() > 0 && surveyAnswersVO.getCurrentTopicID() != null){
				surveyAnswersVO.setNextTopicID(topicID);
				break;
			}
			else if(answersList != null && answersList.size() > 0){
				surveyAnswersVO.setPreviousTopicID(0);
				surveyAnswersVO.setCurrentTopicID(topicID);
				surveyAnswersVO.setNextTopicID(0);
				surveyAnswersVO.setTopicName(topics.getTopicname());
				surveyAnswersVO.setSurveyAnswers(answersList);
			}
		}
		return surveyAnswersVO;
	}

	@RequestMapping(value="/surveyordetails/update", method=RequestMethod.GET)
    public String updateAnswers(SurveyAnswersVO surveyAnswersVO, HttpServletRequest r){
    	LOG.info("factor"+surveyAnswersVO);
    	for(SurveyAnswers surveyAnswers:surveyAnswersVO.getSurveyAnswers()){
        	LOG.info("factor"+surveyAnswers.getAnswercode());
    		List<SurveyAnswers> surveyAnswer = surveyAnswersDAO.selectSurveyAnswersBySurveyQuestionID(surveyAnswers.getSurveyquestionid(),SecurityContextHolder.getContext().getAuthentication().getName());
    		surveyAnswers.setUsername(SecurityContextHolder
					.getContext().getAuthentication().getName());
    		if(surveyAnswer != null && surveyAnswer.size() > 0){
    			surveyAnswersDAO.updateByQuestionID(surveyAnswers);
    		}else{
    			surveyAnswersDAO.insertSurveyAnswers(surveyAnswers);
    		}
    	}
    	if(StringUtils.equals("Y",surveyAnswersVO.getFinishButtonClicked())){
    		return "redirect:/surveyordetails/view.html";
    	}else{
        	r.getSession().setAttribute("surveyAnswersVO", surveyAnswersVO);
    		return "redirect:/surveyordetails/edit.html?surveyId="+surveyAnswersVO.getSurveyID();
    	}
    }
	public void setSurveyDetailsDAO(SurveyDetailsDAO surveyDetailsDAO) {
		this.surveyDetailsDAO = surveyDetailsDAO;
	}
	public void setSurveyAnswersDAO(SurveyAnswersDAO surveyAnswersDAO) {
		this.surveyAnswersDAO = surveyAnswersDAO;
	}
	public void setTopicsDAO(TopicsDAO topicsDAO) {
		this.topicsDAO = topicsDAO;
	} 
}
